package cn.wizzer.app.web.commons.ext.websocket.action.live;

import cn.wizzer.app.match.modules.models.constant.Fields;
import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.user.modules.services.ChatReportService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.app.web.commons.utils.RedisUtil;
import cn.wizzer.app.web.commons.utils.TokenUtilImpl;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import lombok.val;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import static cn.wizzer.app.user.modules.models.constant.ChatConstant.*;

/**
 * @author 22099
 */
@IocBean
@NettyAction(uri = "/send")
public class SendAction extends BaseNettyAction {

    @Inject
    private RedisService redisService;

    @Inject
    UserService userService;

    @Inject
    TokenUtilImpl tokenUtil;

    @Reference
    @Inject
    SysSensitiveService sensitiveService;

    @Reference
    @Inject
    ChatReportService chatReportService;

    public static BaseNettyAction instance() {
        return BaseNettyAction.instance(SendAction.class);
    }

    @Override
    public Result action(Channel income, JSONObject params) {
        //region用户信息
        String userStr = RedisUtil.hget(CHAT_USER, income.id().toString());
        User user = JSONUtil.parseStringToJavaBean(User.class, userStr);
        //endregion
        //region 后台禁言设置
        val enables = RedisUtil.hgetAll(SESSION_ENABLE_USERS);
        if (enables.containsKey(user.getId())) {
            //禁言或允许
            user.setDisabled(!enables.get(user.getId()).equals(Fields.TRUE_STR));
            RedisUtil.hset(CHAT_USER,  user.getChannelId(), user);
            RedisUtil.hdel(SESSION_ENABLE_USERS,user.getId());
        }
        //endregion
        //region 禁言判断/身份判断
        if (user.getRoomId().equals(DEFAULT)){
            return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.ERROR, "尚未加入房间");
        }
        if (user.getId().equals(user.getChannelId())){
            return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.ERROR, "游客用户禁止发言");
        }
        if (user.isSessionDisabled()) {
            return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.REMAND, "您已被禁言");
        }
        //endregion
        //region 房间实例
        //房间哈
        String roomId = RedisUtil.hget(User.class,ChatConstant.CHAT_USER, income.id().toString()).getRoomId();
        ChannelGroup channels = NettyMsgUtil.getLiveRoomChannels(roomId,income);
        //endregion
        String text = params.get(CONTENT).toString();
        //验证敏感词
        text = sensitiveService.filter(text);
        for (Channel channel : channels) {
            if (channel != income) {
                NettyMsgUtil.send(channel, ChatResult.MessageTypeEnum.USER, ChatResult.ActionEnum.SEND, user, text);
            } else {
                NettyMsgUtil.send(channel, ChatResult.MessageTypeEnum.SELF, ChatResult.ActionEnum.SEND, user, text);
            }
        }
        return Result.success();
    }

    @Override
    public Result subscribe(String subscribeParam) {
        return Result.success();
    }



}
